package org.firefly.fx.service.product.impl;

import java.util.List;

import org.firefly.fx.bean.Product;
import org.firefly.fx.dao.ProductDao;
import org.firefly.fx.service.common.IWebsocketSessionService;
import org.firefly.fx.service.product.IProductService;
import org.firefly.fx.util.JSonUtil;
import org.firefly.fx.vo.RouteMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service(value="productService")
public class ProductServiceImpl implements IProductService {
    private Logger log =  LoggerFactory.getLogger(ProductServiceImpl.class);
    
	@Autowired 
	private IWebsocketSessionService wssService;
	
	@Autowired
	private ProductDao productDao;
	
	@Override
	@Scheduled(fixedRate=1000)
	public void sendProductsToUsers() {
		List<Product> products = productDao.getAllProducts();
		
		RouteMessage routeMsg = new RouteMessage();
		routeMsg.setService("productService");
		routeMsg.setParam(JSonUtil.toJSonString(products));
		log.debug("Send products info to all users");
		log.debug(JSonUtil.toJSonString(routeMsg));
		wssService.sendMsgToUsers(JSonUtil.toJSonString(routeMsg));
	}	
}
